package my.ts.code;

import java.lang.reflect.Array;
import java.util.ArrayList;

import my.ts.code.adapter.CategoryAdapter;
import my.ts.code.adapter.HeaderAdapter;
import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.AppContext;
import my.ts.code.objects.Item;
import my.ts.code.objects.Root;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class HeaderScreen extends BaseActivity implements ConfirmListener {

	TextView tvTitle;
	ListView listview;
	HeaderAdapter adapter;
	Root root;
	String sid = "";
	int id;
	String title;
	int layer = -1;
	String n = "10";
	int p = 1;
	private String syntax = "";
	private Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.category_screen);
		context = this;
		setCListener(this);
		init();

		getData(id, sid, n, p);
	}

	@Override
	public void onResume() {

		super.onResume();
		AppContext.instance.session.setListener(this);

	}

	public void getData(int id, String sid, String n, int p) {
		showDialogLoading(this, "Loading Data");
		switch (id) {
		case Config.TIN_TUC:
			AppContext.instance.session.getHeaderNews(sid);
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			break;
		case Config.TRUYEN:
			AppContext.instance.session.getHeaderStory(sid);
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			break;

		case Config.HINH_ANH:
			if (layer == 2) {
				AppContext.instance.session.getImageAlbum(sid);
				tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			} else if (layer == 1) {
				AppContext.instance.session
						.getAlbums(n, String.valueOf(p), sid);
				tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			}
			syntax = Config.IMAGE_SYNTAX.getSyntax();
			break;

		case Config.MUSIC:
			if (layer == 2) {
				AppContext.instance.session.getMusics(sid);
				tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			} else if (layer == 1) {
				AppContext.instance.session.getMusicAlbum(n, String.valueOf(p),
						sid);
				tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			}
			syntax = Config.MUSIC_SYNTAX.getSyntax();
			break;

		case Config.GAME:
			AppContext.instance.session.getGames(n, String.valueOf(p), sid);
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			syntax = Config.GAME_SYNTAX.getSyntax();
			break;

		case Config.VIDEO:
			AppContext.instance.session.getVideos(n, String.valueOf(p), sid);
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			syntax = Config.CLIP_SYNTAX.getSyntax();
			break;

		case Config.AUDIO:
			AppContext.instance.session
					.getAudioAlbum(n, String.valueOf(p), sid);
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			syntax = Config.AUDIO_SYNTAX.getSyntax();
			break;
		case Config.LOTTERY:
			AppContext.instance.session.getLotteryProvince();
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			break;
		case Config.POSTCARD:
			AppContext.instance.session.getPostCardAlbumDetail(sid, n,
					String.valueOf(p));
			tvTitle.setBackgroundResource(R.drawable.tit_bgr_all);
			syntax = Config.POSTCARD_SYNTAX.getSyntax();
			break;
		default:
			break;
		}
	}

	public void init() {

		tvTitle = (TextView) findViewById(R.id.header);
		tvTitle.setVisibility(View.VISIBLE);

		Bundle b = getIntent().getExtras();
		sid = b.getString("sid");
		id = b.getInt("id");
		title = b.getString("title");
		layer = b.getInt("layer");

		tvTitle.setText(title);

		listview = (ListView) findViewById(R.id.list);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parents, View arg1,
					int postion, long idex) {
				if (root.getItems().get(postion).getMore() == 1) {
					getData(id, sid, n, p + 1);
					p += 1;
					root.getItems().remove(root.getItems().size() - 1);
					adapter.notifyDataSetChanged();
				} else if (root.getItems().get(postion).getMore() == 0) {
					switch (id) {
					case Config.GAME:
						Intent intentg = new Intent(HeaderScreen.this,
								DescriptionDetailScreen.class);
						Bundle bg = new Bundle();
						bg.putInt("id", id);
						bg.putString("sid", root.getItems().get(postion)
								.getHeaderID());
						bg.putString("title", root.getItems().get(postion)
								.getHeaderName());

						startActivity(intentg.putExtras(bg));
						break;
					case Config.HINH_ANH:
						if (layer == 1) {
							Intent intentm = new Intent(HeaderScreen.this,
									HeaderScreen.class);
							Bundle bm = new Bundle();
							bm.putInt("id", id);
							bm.putInt("layer", 2);
							bm.putString("title", root.getItems().get(postion)
									.getHeaderName());
							bm.putString("sid", root.getItems().get(postion)
									.getHeaderID());
							startActivity(intentm.putExtras(bm));
						} else {
							Intent intentm = new Intent(HeaderScreen.this,
									ShowPic.class);
							Bundle bm = new Bundle();

							ArrayList<String> ll = new ArrayList<String>();
							ArrayList<String> codes = new ArrayList<String>();
							int s = root.getItems().size();
							for (int i = 0; i < s; i++) {
								ll.add(root.getItems().get(i).getUrlSize3()
										.replace(" ", "%20"));
								codes.add(root.getItems().get(i).getHeaderID());
							}

							bm.putInt("post", postion);
							bm.putInt("amount", s);
							bm.putStringArrayList("list", ll);
							bm.putStringArrayList("sms", codes);
							startActivity(intentm.putExtras(bm));

						}
						break;
					case Config.MUSIC:
						if (layer == 1) {
							Intent intentm = new Intent(HeaderScreen.this,
									HeaderScreen.class);
							Bundle bm = new Bundle();
							bm.putInt("id", id);
							bm.putInt("layer", 2);
							bm.putString("title", root.getItems().get(postion)
									.getHeaderName());
							bm.putString("sid", root.getItems().get(postion)
									.getHeaderID());
							startActivity(intentm.putExtras(bm));

						} else {
							Intent im = new Intent(HeaderScreen.this,
									MusicPlayer.class);
							Bundle bm = new Bundle();

							ArrayList<String> ls = new ArrayList<String>();
							ArrayList<String> ll = new ArrayList<String>();
							ArrayList<String> ld = new ArrayList<String>();

							int s = root.getItems().size();
							for (int i = 0; i < s; i++) {
								ls.add(root.getItems().get(i).getHeaderName());
								ll.add(root.getItems().get(i)
										.getListenTrialURL()
										.replace(" ", "%20"));
								ld.add(root.getItems().get(i)
										.getSMSDownloadCode());
							}

							bm.putStringArrayList("title", ls);
							bm.putStringArrayList("link", ll);
							bm.putStringArrayList("download", ld);
							bm.putInt("position", postion);
							bm.putInt("id", id);

							startActivity(im.putExtras(bm));
						}
						break;

					case Config.VIDEO:
						Intent intentv = new Intent(HeaderScreen.this,
								VideoPlayer.class);
						Bundle bv = new Bundle();
						bv.putString("sid", root.getItems().get(postion)
								.getHeaderID());
						bv.putString("title", root.getItems().get(postion)
								.getHeaderName());
						startActivity(intentv.putExtras(bv));
						break;

					case Config.TIN_TUC:
						ArrayList<String> titles = new ArrayList<String>();
						ArrayList<String> sids = new ArrayList<String>();
						int s = root.getItems().size();
						for (int i = 0; i < s; i++)
							if (root.getItems().get(i).getMore() == 0) {
								titles.add(root.getItems().get(i)
										.getHeaderName());
								sids.add(root.getItems().get(i).getHeaderID());
							}

						Intent intent = new Intent(HeaderScreen.this,
								ContentScreen.class);
						Bundle b = new Bundle();
						b.putInt("id", id);
						b.putInt("pos", postion);

						b.putStringArrayList("sid", sids);
						b.putStringArrayList("title", titles);

						startActivity(intent.putExtras(b));
						break;

					case Config.TRUYEN:
						ArrayList<String> titlest = new ArrayList<String>();
						ArrayList<String> sidst = new ArrayList<String>();
						int st = root.getItems().size();
						for (int i = 0; i < st; i++)
							if (root.getItems().get(i).getMore() == 0) {
								titlest.add(root.getItems().get(i)
										.getHeaderName());
								sidst.add(root.getItems().get(i).getHeaderID());
							}

						Intent intentt = new Intent(HeaderScreen.this,
								ContentScreen.class);
						Bundle bt = new Bundle();
						bt.putInt("id", id);
						bt.putInt("pos", postion);

						bt.putStringArrayList("sid", sidst);
						bt.putStringArrayList("title", titlest);

						startActivity(intentt.putExtras(bt));

						break;
					case Config.POSTCARD:
						Intent intentp = new Intent(HeaderScreen.this,
								DescriptionDetailScreen.class);
						Bundle bp = new Bundle();
						bp.putInt("id", id);
						bp.putString("sid", root.getItems().get(postion)
								.getHeaderID());
						bp.putString("title", root.getItems().get(postion)
								.getHeaderName());

						startActivity(intentp.putExtras(bp));
						break;
					case Config.AUDIO:
						Intent im = new Intent(HeaderScreen.this,
								MusicPlayer.class);
						Bundle bm = new Bundle();

						ArrayList<String> ls = new ArrayList<String>();
						ArrayList<String> ll = new ArrayList<String>();
						ArrayList<String> ld = new ArrayList<String>();

						int sa = root.getItems().size();
						for (int i = 0; i < sa; i++) {
							ls.add(root.getItems().get(i).getHeaderName());
							ll.add(root.getItems().get(i).getListenTrialURL()
									.replace(" ", "%20"));
							ld.add(root.getItems().get(i).getSMSDownloadCode());
						}

						bm.putStringArrayList("title", ls);
						bm.putStringArrayList("link", ll);
						bm.putStringArrayList("download", ld);
						bm.putInt("position", postion);
						bm.putInt("id", id);

						startActivity(im.putExtras(bm));
						break;

					case Config.LOTTERY:
						if (sid.equals("0")) {
							ArrayList<String> titlesl = new ArrayList<String>();
							ArrayList<String> sidsl = new ArrayList<String>();
							int sl = root.getItems().size();
							for (int i = 0; i < sl; i++)
								if (root.getItems().get(i).getMore() == 0) {
									titlesl.add(root.getItems().get(i)
											.getHeaderName());
									sidsl.add(root.getItems().get(i).getHeaderID());
								}

							Intent intentl = new Intent(HeaderScreen.this,
									ContentScreen.class);
							Bundle bl = new Bundle();
							bl.putInt("id", id);
							bl.putInt("pos", postion);

							bl.putStringArrayList("sid", sidsl);
							bl.putStringArrayList("title", titlesl);

							startActivity(intentl.putExtras(bl));

						} else if (sid.equals("1")) {
							if (root.getItems().get(postion).getHeaderID()
									.equals("1"))
								showDialogConfirmSMS(context,
										HeaderScreen.this,
										"Gửi tin nhắn để soi cầu?",
										"Thông báo",
										Config.LOTTERY_SYNTAX.getSyntax()
												+ "MB",
										Config.LOTTERY_SYNTAX.getNumber(),
										false, false);
							else
								showDialogConfirmSMS(context,
										HeaderScreen.this,
										"Gửi tin nhắn để soi cầu?",
										"Thông báo",
										Config.LOTTERY_SYNTAX.getSyntax()
												+ root.getItems().get(postion)
														.getHeaderID(),
										Config.LOTTERY_SYNTAX.getNumber(),
										false, false);
						}
						break;

					default:
						break;
					}
				}

			}
		});
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {

				Root r = (Root) msg.obj;
				if (r != null)
					if (r.getMessage() != null) {

						showDialogConfirmSMS(context, HeaderScreen.this,
								r.getMessage(), "Thông báo",
								Config.CHARGE_7_SYNTAX.getSyntax() + "15000 "
										+ Config.TSM,
								Config.CHARGE_7_SYNTAX.getNumber(), false, true);

					} else {

						if (p == 1) {
							root = r;
							root.setSyntax(syntax);
							if (id == Config.GAME || id == Config.VIDEO
									|| id == Config.POSTCARD
									|| (layer == 2 && id == Config.HINH_ANH)) {
								adapter = new HeaderAdapter(getBaseContext(),
										1, root.getItems(), syntax);

							} else {
								adapter = new HeaderAdapter(getBaseContext(),
										0, root.getItems(), syntax);
							}

							listview.setAdapter(adapter);
						} else {
							root.setMore(r.isMore());
							// root.getItems().remove(root.getItems().size() -
							// 1);
							int s = r.getItems().size();
							for (int i = 0; i < s; i++)
								root.getItems().add(r.getItems().get(i));
						}

						if (root.isMore()) {
							Item item = new Item();
							item.setMore(1);
							root.getItems().add(item);
						}

						adapter.notifyDataSetChanged();
					}
			}
		}
	};

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub
		// System.out.println("ON DISPATCH");

		dialog.dismiss();

		Root root = parse(data);

		if (root != null) {
			Message msg = handler.obtainMessage(1, root);
			handler.sendMessage(msg);
		} else {
			if (p == 1) {

			} else if (p > 1) {
				Item item = new Item();
				item.setMore(1);
				root.getItems().add(item);
				adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void confirmListener(boolean done) {
		// TODO Auto-generated method stub
		if (done) {
			showDialogLoading(context, "Loading data");
			Thread t = new Thread() {
				public void run() {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					getData(id, sid, n, p);
				}
			};

			t.start();
		} else
			finish();

	}
}
